package org.example.SolutionNew003;

import java.util.Stack;

public class DoubleTree008 {
    public static void main(String[] args) {
        DoubleTree008 dt = new DoubleTree008();
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(-2147483648);
        root.right = new TreeNode(2147483647);
//        root.right.right = new TreeNode(7);
//        root.left.right = new TreeNode(3);
        System.out.println(dt.isValidBST(root));
    }

    //遍历
    public boolean isValidBST1(TreeNode root) {
        return isBST(root,Long.MIN_VALUE,Long.MAX_VALUE);
    }

    public boolean isBST(TreeNode root, Long min, Long max) {
        if (root == null) return true;
        if (root.val <= min || root.val >= max) return false;
        return isBST(root.left, min,  (long)root.val) && isBST(root.right, (long)root.val, max);
    }

    //中序遍历
    public boolean isValidBST(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        double inorder=-Double.MAX_VALUE;

        while(!stack.isEmpty() || root != null){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            root =stack.pop();
            if(root.val <= inorder) return false;
            inorder = root.val;
            root = root.right;
        }
        return true;
    }
}
